#include "Core.h"
#include "WinHelper.h"
#include <windowsx.h>
#include <commctrl.h>
using namespace std;

///////////////////////////////////////////////////////////////////
//Codes

#define CMD_ABOUT 501
#define CMD_TRAINING 502
#define CMD_RESULTS 503
#define CMD_HELP 504
#define CMD_EXIT 505

#define CMD_NEXT_WORD 601
#define CMD_START_TRAINING 602
#define CMD_RESET_TRAINING 603

///////////////////////////////////////////////////////////////////
//Data types

enum TSEMode
{
    TSEM_SETTINGS, TSEM_NORMAL
};

///////////////////////////////////////////////////////////////////
//Prototypes

void InitWindows();
void ReleaseWindows();
void ShowMainWindow();
void HideMainWindow();

void LoadLibs();
void FlushLibs();

void InitTraining();
void ResetTraining();
void PauseTraining();
void ContinueTraining();
void StartTraining();
void StopTraining();
bool IsTrainingRun();
bool IsTrainingPaused();
void UpdateResults();


void TrShowElements(TSEMode mode);
void TrNextWord();
void TrWordStatus(bool st, wstring right = L"");
bool TrSetNewWord();

///////////////////////////////////////////////////////////////////
//Structures of data

struct Windows
{
	HWND MainWindow;
	HWND TrainWindow;
	HWND Results;
	HWND Help;
	HWND About;
} w;

struct MainWindowElements
{
	HWND ButtonAbout;
	HWND ButtonTraining;
	HWND ButtonResults;
	HWND ButtonHelp;
	HWND ButtonExit;

	HWND LeftImage;
} mwe;

struct TrainingWindowElements
{
	HWND label_jp;
	HWND edit_romaji;
	HWND label_last_status;
	HWND button_next;

    HWND button_start;
	HWND button_reset;
	HWND check_box_hira;
	HWND check_box_kata;
} twe;

struct ResultWindowElements
{
	HWND label_total_words;
	HWND edit_total_words;
	HWND label_right_words;
	HWND edit_right_words;
	HWND label_elapsed_time;
	HWND edit_elapsed_time;
	HWND label_time_per_word;
	HWND edit_time_per_word;
} rwe;

struct HelpWindowElements
{

} hwe;

struct AboutWindowElements
{
    HWND LinkGroup;
    HWND InfoAuthor;
    HWND InfoTeam;
} awe;

///////////////////////////////////////////////////////////////////
//Styles

DWORD style = WS_CAPTION | WS_SYSMENU | WS_POPUPWINDOW | WS_MINIMIZEBOX;
DWORD chv_style = WS_VISIBLE | WS_CHILD;
DWORD simple_button_style = BS_TEXT | BS_CENTER;
DWORD simple_link_style = 0;
DWORD edit_style = 0;
DWORD edit_style_ex = WS_EX_CLIENTEDGE;
DWORD label_style = SS_CENTER;
DWORD label_style_ex = WS_EX_CLIENTEDGE;
DWORD check_box_style = BS_CHECKBOX | BS_AUTOCHECKBOX;

///////////////////////////////////////////////////////////////////
//App

void InitApp()
{
    ZeroMemory(&w, sizeof(w));
    InitWindows();
    LoadLibs();
    ShowMainWindow();
}

void ReleaseApp()
{

}

///////////////////////////////////////////////////////////////////
//Events

bool EventCloseWindow(HWND hWnd)
{
	if (w.TrainWindow == hWnd && IsTrainingRun())
	{
		PauseTraining();
        int i = MessageBoxExW(hWnd, L"Закончить тренировку?", L"", MB_YESNO, 0);
        if (i == IDYES)
			StopTraining();
		else
		{
			ContinueTraining();
			return false;
		}
	}
	ShowWindow(hWnd, SW_HIDE);
	if (hWnd != NULL && w.MainWindow == hWnd)
		return true;
	ShowWindow(w.MainWindow, SW_SHOW);
	WindowToScreenCenter(w.MainWindow, true);
	return false;
}

bool EventCommand(HWND hWnd, WPARAM wParam)
{
	switch (wParam)
	{
	case CMD_ABOUT:
		UpdateWindow(w.About);
		ShowWindow(w.About, SW_SHOWNORMAL);
		ShowWindow(w.MainWindow, SW_HIDE);
		WindowToScreenCenter(w.About);
		break;
	case CMD_TRAINING:
		UpdateWindow(w.TrainWindow);
		ShowWindow(w.TrainWindow, SW_SHOWNORMAL);
		ShowWindow(w.MainWindow, SW_HIDE);
		WindowToScreenCenter(w.TrainWindow);
		break;
	case CMD_RESULTS:
		UpdateResults();
		UpdateWindow(w.Results);
		ShowWindow(w.Results, SW_SHOWNORMAL);
		ShowWindow(w.MainWindow, SW_HIDE);
		WindowToScreenCenter(w.Results);
        break;
	case CMD_HELP:
		UpdateWindow(w.Help);
		ShowWindow(w.Help, SW_SHOWNORMAL);
		ShowWindow(w.MainWindow, SW_HIDE);
		WindowToScreenCenter(w.Help);
		break;
	case CMD_EXIT:
		PostQuitMessage(0);
		break;

	case CMD_START_TRAINING:
        StartTraining();
		break;
	case CMD_RESET_TRAINING:
		ResetTraining();
		break;
	case CMD_NEXT_WORD:
        TrNextWord();
		break;
	default:
        return false;
	}
    return true;
}

bool EventNotify(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    NMHDR *n = (NMHDR *) lParam;
    if (!n)
		return false;
    switch (n->code)
    {
	case NM_CLICK:
		if (n->hwndFrom == awe.LinkGroup || n->hwndFrom == awe.InfoAuthor)
			OpenLink(lParam);
		break;
	case NM_RETURN:
		if (n->hwndFrom == awe.LinkGroup || n->hwndFrom == awe.InfoAuthor)
            OpenLink(lParam);
		break;
	default:
		break;
    }
	return true;
}

void EventWindowLoseFocus(HWND hWnd)
{
    if (hWnd == w.About ||
			hWnd == w.Help ||
			hWnd == w.Results ||
			hWnd == w.TrainWindow)
	{
		if (!IsWindowVisible(w.MainWindow))
			SendMessageW(hWnd, WM_CLOSE, 0, 0);
		//int i = 0;
	}
}

///////////////////////////////////////////////////////////////////
//Windows Functions

void InitMainWindow();
void InitAboutWindow();
void InitHelpWindow();
void InitTrainWindow();
void InitResultWindow();

void InitWindows()
{
	InitMainWindow();
	InitAboutWindow();
	InitHelpWindow();
	InitTrainWindow();
	InitResultWindow();

	InitTraining();
}


void InitMainWindow()
{
	int width = 500, height = 350,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.MainWindow =
		CreateWindowExW(0, L"stdclass", L"鉛筆 — Enpitsu", style,
						posx, posy, width, height, NULL, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.MainWindow, &client_rect);
	int btn_width = 200, btn_heigh = 30;
	/////////////////////////////////////////
	int button_right_spacing = client_rect.right - btn_width - 20;
	int btn_about_posx = button_right_spacing;
	int btn_about_posy = 25;

	mwe.ButtonAbout =
    CreateWindowExW(0, L"button", L"Об Авторе", simple_button_style | chv_style,
					btn_about_posx, btn_about_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_ABOUT, hInst, NULL);
	SetDefaultFont(mwe.ButtonAbout);
	/////////////////////////////////////////
	int btn_training_posx = button_right_spacing;
	int btn_training_posy = btn_about_posy + btn_heigh + 30;

	mwe.ButtonTraining =
    CreateWindowExW(0, L"button", L"Тренировка", simple_button_style | chv_style,
					btn_training_posx, btn_training_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_TRAINING, hInst, NULL);
	SetDefaultFont(mwe.ButtonTraining);
	/////////////////////////////////////////
	int btn_results_posx = button_right_spacing;
	int btn_results_posy = btn_training_posy + btn_heigh + 30;

	mwe.ButtonResults =
    CreateWindowExW(0, L"button", L"Результаты", simple_button_style | chv_style,
					btn_results_posx, btn_results_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_RESULTS, hInst, NULL);
	SetDefaultFont(mwe.ButtonResults);
	/////////////////////////////////////////
	int btn_help_posx = button_right_spacing;
	int btn_help_posy = btn_results_posy + btn_heigh + 30;

	mwe.ButtonHelp =
    CreateWindowExW(0, L"button", L"Помощь", simple_button_style | chv_style,
					btn_help_posx, btn_help_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_HELP, hInst, NULL);
	SetDefaultFont(mwe.ButtonHelp);
	/////////////////////////////////////////
	int btn_exit_posx = button_right_spacing;
	int btn_exit_posy = btn_help_posy + btn_heigh + 30;

	mwe.ButtonExit =
    CreateWindowExW(0, L"button", L"Выход", simple_button_style | chv_style,
					btn_exit_posx, btn_exit_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_EXIT, hInst, NULL);
	SetDefaultFont(mwe.ButtonExit);
	/////////////////////////////////////////


	/////////////////////////////////////////
	UpdateWindow(w.MainWindow);
	SetDefaultFont(w.MainWindow);
}

void InitAboutWindow()
{
	int width = 400, height = 300,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.About =
		CreateWindowExW(0, L"stdclass", L"Об Авторе", style,
						posx, posy, width, height, w.MainWindow, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.About, &client_rect);
	/////////////////////////////////////////
	int link_x, link_y, link_sx, link_sy;
    link_sx = 200;
    link_sy = 15;
    link_x = client_rect.left + 10;
    link_y = client_rect.bottom - 10 - link_sy;
	awe.LinkGroup =
		CreateWindowExW(0, WC_LINKW,
						L"VK Group: <a href=\"http://vk.com/nihon.rosia\" >日本 • Русь</a>",
						simple_link_style | chv_style,
						link_x, link_y, link_sx, link_sy, w.About, NULL, hInst, NULL);
    SetDefaultFont(awe.LinkGroup);
    /////////////////////////////////////////
    int ax, ay, asx, asy;
    asx = 200;
    asy = 20;
    ax = client_rect.right/2 - asx/2;
    ay = client_rect.top + 10;
    awe.InfoAuthor =
		CreateWindowExW(0, WC_LINKW,
						L"Author: <a href=\"http://vk.com/el8_grey_wolf\" >GrWolf</a>",
						simple_link_style | chv_style ,
						ax, ay, asx, asy, w.About, NULL, hInst, NULL);
	SetDefaultFont(awe.InfoAuthor);
	SetFontSize(awe.InfoAuthor, 24);
    /////////////////////////////////////////
    ay += asy + 5;
    awe.InfoTeam =
		CreateWindowExW(0, L"static", L"Endlessness © 2015", chv_style,
						ax, ay, asx, asy, w.About, NULL, hInst, NULL);
	SetDefaultFont(awe.InfoTeam);
	SetFontSize(awe.InfoTeam, 24);
    /////////////////////////////////////////
	UpdateWindow(w.About);
	SetDefaultFont(w.About);
	/////////////////////////////////////////
}

void InitHelpWindow()
{
	int width = 400, height = 300,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.Help =
		CreateWindowExW(0, L"stdclass", L"Помощь", style,
						posx, posy, width, height, w.MainWindow, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.Help, &client_rect);
	/////////////////////////////////////////
}

void InitTrainWindow()
{
	int width = 800, height = 250,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.TrainWindow =
		CreateWindowExW(0, L"stdclass", L"Тренировка каны", style,
						posx, posy, width, height, w.MainWindow, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.TrainWindow, &client_rect);
    int cx = (client_rect.right - client_rect.left)/2;
    int cy = (client_rect.bottom - client_rect.top)/2;
	/////////////////////////////////////////
	int label_x, label_y, label_sx, label_sy;
	label_sy = 70;
    label_x = client_rect.left + 10;
    label_y = cy - label_sy/2;
    label_sx = cx - label_x - 10;
    twe.label_jp =
		CreateWindowExW(label_style_ex, L"static", L"日本", label_style | chv_style,
						label_x, label_y, label_sx, label_sy, w.TrainWindow, NULL, hInst, NULL);
	SetDefaultFont(twe.label_jp);
	SetFontSize(twe.label_jp, 72);
	/////////////////////////////////////////
	int edit_x, edit_y, edit_sx, edit_sy;
    edit_x = label_x + label_sx + 10;
    edit_sx = client_rect.right - edit_x - 10;
    edit_y = label_y + 20;
    edit_sy = label_sy - 20;
	twe.edit_romaji =
		CreateWindowExW(edit_style_ex, L"edit", L"nihon", edit_style | chv_style | ES_WANTRETURN,
						edit_x, edit_y, edit_sx, edit_sy, w.TrainWindow, NULL, hInst, NULL);
	SetDefaultFont(twe.edit_romaji);
	SetFontSize(twe.edit_romaji, 48);
	/////////////////////////////////////////
	int ls_x, ls_y, ls_sx, ls_sy;
    ls_sx = 500;
    ls_sy = 15;
    ls_x = 10;
    ls_y = client_rect.bottom - ls_sy - 10;
    twe.label_last_status =
		CreateWindowExW(0, L"static", L"Слов не было с:", chv_style,
						ls_x, ls_y, ls_sx, ls_sy,
						w.TrainWindow, NULL, hInst, NULL);
	SetDefaultFont(twe.label_last_status);
	/////////////////////////////////////////
	int nb_x, nb_y, nb_sx, nb_sy;
    nb_sx = 300;
    nb_sy = 30;
    nb_x = client_rect.right - nb_sx - 10;
	nb_y = client_rect.bottom - nb_sy - 10;
	twe.button_next =
		CreateWindowExW(0, L"button", L"Следующее слово", simple_button_style | chv_style,
						nb_x, nb_y, nb_sx, nb_sy, w.TrainWindow, (HMENU) CMD_NEXT_WORD, hInst, NULL);
	SetDefaultFont(twe.button_next);
	/////////////////////////////////////////
	/////////////////////////////////////////
	int sb_x, sb_y, sb_sx, sb_sy;
	sb_sx = 300;
	sb_sy = 30;
	sb_x = client_rect.right - sb_sx - 10;
	sb_y = client_rect.bottom - sb_sy - 10;
    twe.button_start =
		CreateWindowExW(0, L"button", L"Начать тренировку!", simple_button_style | chv_style,
						sb_x, sb_y, sb_sx, sb_sy, w.TrainWindow, (HMENU) CMD_START_TRAINING, hInst, NULL);
	SetDefaultFont(twe.button_start);
	/////////////////////////////////////////
	int rb_x, rb_y, rb_sx, rb_sy;
	rb_sx = 300;
	rb_sy = 30;
	rb_x = client_rect.right - rb_sx - 10;
	rb_y = sb_y - rb_sy - 10;
    twe.button_reset =
		CreateWindowExW(0, L"button", L"Сбросить открытые слова", simple_button_style | chv_style,
						rb_x, rb_y, rb_sx, rb_sy, w.TrainWindow, (HMENU) CMD_RESET_TRAINING, hInst, NULL);
	SetDefaultFont(twe.button_reset);
	/////////////////////////////////////////
	int chbox_x, chbox_y, chbox_sx, chbox_sy;
    chbox_x = client_rect.left + 10;
    chbox_y = client_rect.top + 10;
    chbox_sx = 200;
    chbox_sy = 25;
    twe.check_box_hira =
		CreateWindowExW(0, L"button", L"Словарь Хираганы", check_box_style | chv_style,
						chbox_x, chbox_y, chbox_sx, chbox_sy, w.TrainWindow, NULL, hInst, NULL);
	chbox_y = chbox_sy + 5;
	twe.check_box_kata =
		CreateWindowExW(0, L"button", L"Словарь Катаканы", check_box_style | chv_style,
						chbox_x, chbox_y, chbox_sx, chbox_sy, w.TrainWindow, NULL, hInst, NULL);
	SetDefaultFont(twe.check_box_hira);
	SetDefaultFont(twe.check_box_kata);
	Button_SetCheck(twe.check_box_hira, BST_CHECKED);
	Button_SetCheck(twe.check_box_kata, BST_CHECKED);
	/////////////////////////////////////////
}

void CreateResultTableRow(HWND parent, HWND &label, HWND &edit, wstring name, size_t row)
{
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	RECT client_rect;
	GetClientRect(parent, &client_rect);
	row--;
	int margin = 10;
	int sx = client_rect.right/2 - 15, sy = 25;
	int x = client_rect.left + 10;
	int y = client_rect.top + (sy + margin) * row + margin;

	label = CreateWindowExW(0, L"static", name.data(), label_style | chv_style | SS_CENTER,
							x, y, sx, sy, parent, NULL, hInst, NULL);
	x += sx + 10;
	edit = CreateWindowExW(edit_style_ex, L"edit", L"0", edit_style | chv_style | ES_READONLY,
							x, y, sx, sy, parent, NULL, hInst, NULL);
	SetDefaultFont(label);
	SetDefaultFont(edit);
	SetFontSize(label, 24);
	SetFontSize(edit, 24);
}

void InitResultWindow()
{
	int width = 400, height = 300,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.Results =
		CreateWindowExW(0, L"stdclass", L"Результаты", style,
						posx, posy, width, height, w.MainWindow, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.MainWindow, &client_rect);
	/////////////////////////////////////////
	int i = 1;
	CreateResultTableRow(w.Results, rwe.label_total_words, rwe.edit_total_words,
						L"Всего слов:", i++);
	CreateResultTableRow(w.Results, rwe.label_right_words, rwe.edit_right_words,
						L"Правильно слов:", i++);
	CreateResultTableRow(w.Results, rwe.label_elapsed_time, rwe.edit_elapsed_time,
						L"Затраченное время (с):", i++);
	CreateResultTableRow(w.Results, rwe.label_time_per_word, rwe.edit_time_per_word,
						L"Секунд на слово:", i++);
}

void ReleaseWindows()
{
	if (w.MainWindow)
	{
		DestroyWindow(w.MainWindow);
	}
}

void ShowMainWindow()
{
    ShowWindow(w.MainWindow, SW_SHOW);
}

void HideMainWindow()
{
	ShowWindow(w.MainWindow, SW_HIDE);
}

///////////////////////////////////////////////////////////////////
//Libs functions

void LoadLibs()
{
	LoadWordsLibs();
}

void FlushLibs()
{
	WordsLibrary.hiragana.clear();
	WordsLibrary.katakana.clear();
}

///////////////////////////////////////////////////////////////////
//Training

#include <ctime>

struct training
{
	bool run;
	bool paused;

	word current_word;
	WordType type;

	struct result
	{
        size_t total;
        size_t right;

        long double elapsed_time;
        long double sec_per_word;
	} r;

	struct times
	{
		clock_t total_time;
		clock_t total_start;
		clock_t total_end;
		clock_t word_time;
		clock_t word_time_last;
		clock_t word_start;
		clock_t word_end;
		times()
		{
			total_time = 0;
			total_start = 0;
			total_end = 0;
			word_time = 0;
			word_time_last = 0;
			word_start = 0;
			word_end = 0;
		}

	} t;
	void start()
	{
		run = true;
		t.total_start = clock();
		t.word_start = clock();
	}
	void pause()
	{
		paused = true;
		t.total_end = clock();
		t.total_time += t.total_end - t.total_start;
		t.word_end = clock();
		t.word_time += t.total_end - t.total_start;
	}
	void cont()
	{
		paused = false;
		t.total_start = clock();
		t.word_start = clock();
	}
	void stop()
	{
        run = false;
        paused = false;
		t.total_end = clock();
        t.word_end = t.total_end;
        t.total_time += t.total_end - t.total_start;
        t.word_time += t.total_end - t.total_start;
	}
	void next()
	{
		t.word_time += t.word_end - t.word_start;
		t.word_time_last = t.word_time;
        t.word_time = 0;
	}
	void update_time()
	{
        if (paused || !run)
			return;
        t.word_end = clock();
        t.total_end = clock();
        t.word_time += t.word_end - t.word_start;
        t.total_time += t.total_end - t.total_start;
        t.word_start = t.total_start = clock();
	}
	void update_result()
	{
		r.elapsed_time = t.total_time / 1000;
		r.sec_per_word = (r.sec_per_word + (long double) (t.word_time_last) / 1000)/2;
	}
} tr;

void InitTraining()
{
	tr.r.right = 0;
	tr.r.total = 0;
    tr.paused = false;
    tr.run = false;
    tr.type = WT_BOTH;
    TrShowElements(TSEM_SETTINGS);
}

void ResetTraining()
{
	ResetWordsRandom();
    SetWindowTextW(twe.label_last_status, L"Слов не было с:");
}

void PauseTraining()
{
	if (!tr.run || tr.paused)
		return;
	tr.pause();
	ShowWindow(twe.label_jp, SW_HIDE);
}

void ContinueTraining()
{
	if (!tr.run || !tr.paused)
		return;
	tr.cont();
	ShowWindow(twe.label_jp, SW_SHOW);
}

void StartTraining()
{
	if (IsTrainingRun())
		return;
	int ch1 = Button_GetCheck(twe.check_box_hira), ch2 = Button_GetCheck(twe.check_box_kata);
	if (ch1 == BST_CHECKED || ch2 == BST_CHECKED)
	{
        if (ch1 == BST_CHECKED && ch2 == BST_CHECKED)
			tr.type = WT_BOTH;
		else if (ch1 == BST_CHECKED)
			tr.type = WT_HIRAGANA;
		else if (ch2 == BST_CHECKED)
			tr.type = WT_KATAKANA;
	}
	else
	{
		MessageBoxExW(w.TrainWindow, L"Выбери хотя бы один словарь!", L"Чта?", MB_ICONERROR, 0);
        return;
	}
	tr.start();
	if (!TrSetNewWord())
	{
		StopTraining();
		return;
	}
	TrShowElements(TSEM_NORMAL);
}

void StopTraining()
{
	TrShowElements(TSEM_SETTINGS);
	if (!IsTrainingRun())
		return;
	tr.stop();
}

void UpdateTimer()
{
    tr.update_time();
}

bool IsTrainingRun()
{
	return tr.run;
}

bool IsTrainingPaused()
{
	return tr.paused;
}

#include <cstdio>

void UpdateResults()
{
	char temp[80];
    sprintf(temp, "%e", (double) tr.r.elapsed_time);
    SetWindowTextA(rwe.edit_elapsed_time, temp);
    sprintf(temp, "%e", (double) tr.r.sec_per_word);
    SetWindowTextA(rwe.edit_time_per_word, temp);
    sprintf(temp, "%d", tr.r.total);
    SetWindowTextA(rwe.edit_total_words, temp);
    sprintf(temp, "%d", tr.r.right);
    SetWindowTextA(rwe.edit_right_words, temp);
}

///////////////////////////////////////////////////////////////////

void TrShowElements(TSEMode mode)
{
    switch (mode)
    {
	case TSEM_NORMAL:
        ShowWindow(twe.label_jp, SW_SHOW);
        ShowWindow(twe.edit_romaji, SW_SHOW);
        ShowWindow(twe.label_last_status, SW_SHOW);
        ShowWindow(twe.button_next, SW_SHOW);

        ShowWindow(twe.button_start, SW_HIDE);
        ShowWindow(twe.button_reset, SW_HIDE);
        ShowWindow(twe.check_box_hira, SW_HIDE);
        ShowWindow(twe.check_box_kata, SW_HIDE);
		break;
	case TSEM_SETTINGS:
		ShowWindow(twe.label_jp, SW_HIDE);
        ShowWindow(twe.edit_romaji, SW_HIDE);
        ShowWindow(twe.label_last_status, SW_HIDE);
        ShowWindow(twe.button_next, SW_HIDE);

        ShowWindow(twe.button_start, SW_SHOW);
        ShowWindow(twe.button_reset, SW_SHOW);
        ShowWindow(twe.check_box_hira, SW_SHOW);
        ShowWindow(twe.check_box_kata, SW_SHOW);
		break;
    }
}

void TrNextWord()
{
	if (!tr.run || tr.paused)
		return;
	wstring str = GetStringW(twe.edit_romaji);
	str = RomajiFilter(str);
	wstring cur = RomajiFilter(tr.current_word.romaji);
	tr.r.total++;
	if (str == cur)
	{
		TrWordStatus(true);
		tr.r.right++;
	}
	else
		TrWordStatus(false, tr.current_word.romaji);
    if (TrSetNewWord())
	{
		tr.next();
		tr.update_result();
	}
	else
		StopTraining();
}

void TrWordStatus(bool st, wstring right)
{
	SetWindowTextW(twe.label_last_status, L"");
	if (st)
		SetWindowTextW(twe.label_last_status, L"Правильный ответ.");
	else
		SetWindowTextW(twe.label_last_status, (L"Неправильный ответ. Верный: " + right).data());
    UpdateWindow(twe.label_last_status);
    SendMessageW(twe.label_last_status, WM_PAINT, 0, 0);
}

bool TrSetNewWord()
{
	word wd;
    if (GetRandomWord(tr.type, wd))
	{
        tr.current_word = wd;
        SetWindowTextW(twe.label_jp, wd.kana.data());
        SetWindowTextW(twe.edit_romaji, L"");
        UpdateWindow(twe.label_jp);
        UpdateWindow(twe.edit_romaji);
        SendMessageW(w.TrainWindow, WM_PAINT, 0, 0);
		return true;
	}
	return false;
}
